//
//  ZWTabBarController.swift
//  weiboXue
//
//  Created by xue-iOS-work on 16/8/29.
//  Copyright © 2016年 iOS-work. All rights reserved.
//

import UIKit

class ZWTabBarController: UITabBarController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 因为tabBar是一个只读属性 不可以通过self去赋值
        let tabBar = ZWTabBar()
        // 使用KVC去赋值
        setValue(tabBar,forKey:"tabBar")
        // 设置tabbar的闭包
        tabBar.closeBlock = {[weak self]in
            print("这里是按钮的点击事件\(self)")
            
        }
        
        
        // 加载控制器
        addChildViewControllers()
        
    }
    // 加载所有的控制器
    func addChildViewControllers() {
        addChildViewController(vc: ZWHomeViewController(), title: "首页", image: "tabbar_home")
        addChildViewController(vc: ZWMessageViewController(), title: "消息", image: "tabbar_message")
        addChildViewController(vc: ZWFoundViewController(), title: "发现", image: "tabbar_discover")
        addChildViewController(vc: ZWMeViewController(), title: "我", image: "tabbar_profile")
        
    }
    // 分别加载控制器
    func addChildViewController(vc:UIViewController,title:String,image:String) {
//        self.tabBarItem
        // 设置标题
        vc.title = title
        // 设置标题的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange,NSFontAttributeName:UIFont.systemFont(ofSize: 12)], for: UIControlState.highlighted)
        // 设置图片
        vc.tabBarItem.image = UIImage(named: image)
        vc.tabBarItem.selectedImage = UIImage(named: "\(image)_selected")
        // 设置图片的偏移量
//        vc.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, 5, 0);
        if #available(iOS 10.0, *) {
            vc.tabBarItem.setBadgeTextAttributes([NSForegroundColorAttributeName:UIColor.red], for: .normal)
            vc.tabBarItem.badgeColor = UIColor.black
        } else {
            // Fallback on earlier versions
        }
        // 创建nav
        let nav = ZWNavigationController(rootViewController: vc)
        addChildViewController(nav)// 重载
    }
}
